﻿using UnityEngine;
using System.Collections;
using BehaviorSystem;
public class MoveNode : ActionNode
{
    MoveComponent moveComponent;

    float distance;

    public MoveNode(IBehaviorAgent agent) : base(agent)
    {
        moveComponent = agent.GetComponent<MoveComponent>();
    }

    public override BTNodeState HandleAction()
    {
        if (moveComponent.IsCanMove)
        {
            if (moveComponent != null && moveComponent.TargetPos != Vector3.zero)
            {
                distance = Vector3.Distance(moveComponent.transform.position, moveComponent.TargetPos);
                if (distance > 0.1)
                {
                    moveComponent.transform.LookAt(moveComponent.TargetPos);
                    moveComponent.transform.position = Vector3.MoveTowards(moveComponent.transform.position, moveComponent.TargetPos, moveComponent.Speed);
                    CurState = BTNodeState.Running;
                }
                else
                {
                    moveComponent.TargetPos = Vector3.zero;
                    CurState = BTNodeState.Success;
                }
            }
        }
        else
        {
            CurState = BTNodeState.Failure;
        }

        return CurState;
    }
}
